package com.gdut.simple.view.activity.user;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gdut.simple.R;
import com.gdut.simple.common.CityUtils;
import com.gdut.simple.contract.UpdateInfoContract;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.model.impl.UpdateAreaModelImpl;
import com.gdut.simple.presenter.user.UpdateInfoPresenter;
import com.gdut.simple.view.adapter.AreaAdapter;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

public class CityActivity extends AbsBasicActivity implements UpdateInfoContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_area_list)
    RecyclerView mCityList;

    private String province;
    private String chooseItem;

    private UpdateInfoContract.Presenter mUpdatePresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_area);
        ButterKnife.bind(this);
        new UpdateInfoPresenter(this);
        initToolbar();
    }

    private void initToolbar() {
        province = getIntent().getStringExtra("province");
        mToolbar.setTitle(province);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void loadData() {
        final List<String> cities = CityUtils.getCities(this, province);
        if (cities != null) {
            AreaAdapter adapter = new AreaAdapter(this, cities);
            mCityList.setLayoutManager(new LinearLayoutManager(this));
            mCityList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
            mCityList.setAdapter(adapter);
            adapter.setOnItemClickListener(new AbsRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onClick(View view, int position) {
                    if (province.equals(cities.get(position))) {
                        chooseItem = cities.get(position);
                    } else {
                        chooseItem = province + cities.get(position);
                    }
                    mUpdatePresenter.update(new UpdateInfoContract.UpdateStrategy(new UpdateAreaModelImpl()));
                }

                @Override
                public boolean onLongClick(View view, int position) {
                    return false;
                }
            });
        }
    }

    @Override
    public User getUser() {
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public String getValue() {
        return chooseItem;
    }

    @Override
    public void next() {
        RxBus.getDefault().post(new UserEvent(true));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 100);
    }

    @Override
    public void setPresenter(@NonNull UpdateInfoContract.Presenter presenter) {
        this.mUpdatePresenter = presenter;
    }
}
